<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>003.中文输入法合成事件</title>
  </head>
  <body>
    <input id="input" />

    <script>
      function search() {
        const input = document.getElementById("input");
        const value = input.value;

        console.log(value);
      }

      let isComposing = false;
      document
        .getElementById("input")
        .addEventListener("compositionupdate", (e) => {
          console.log("用户开始输入");
          isComposing = true;
        });

      document
        .getElementById("input")
        .addEventListener("compositionend", (e) => {
          console.log("用户结束输入");
          isComposing = false;
          search();
        });

      document.getElementById("input").addEventListener("input", (e) => {
        if (!isComposing) {
          search();
        }
      });
    </script>
  </body>
</html>
